package com.requiem.hist_group_manage_springboot.service.impl;

import com.requiem.hist_group_manage_springboot.exception.R;
import com.requiem.hist_group_manage_springboot.mapper.DataMapper;
import com.requiem.hist_group_manage_springboot.model.GroupModel;
import com.requiem.hist_group_manage_springboot.service.DataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DataServiceImpl implements DataService {
    @Autowired
    DataMapper dataMapper;

    @Override
    public R getData() {
        // 1. 查询各类统计数据
        int groupCount = dataMapper.countGroup();
        int studentCount = dataMapper.countStudent();
        int teacherCount = dataMapper.countTeacher();
        int logCount = dataMapper.countLog();

        List<GroupModel> hotGroups = dataMapper.getHotGroup();
        List<GroupModel> numberGroups = dataMapper.getNumberGroup();
        List<GroupModel> logGroups = dataMapper.getLogGroup();

        List<Map<String, Object>> sexCounts = dataMapper.getSexCount();
        List<Map<String, Object>> gradeCounts = dataMapper.getGradeCount();

        // 2. 组装结果（使用链式编程）
        Map<String, Object> result = new HashMap<>() {{
            put("group", groupCount);
            put("student", studentCount);
            put("teacher", teacherCount);
            put("log", logCount);
            put("hotGroup", hotGroups != null ? hotGroups : Collections.emptyList());
            put("numberGroup", numberGroups != null ? numberGroups : Collections.emptyList());
            put("logGroup", logGroups != null ? logGroups : Collections.emptyList());
            put("sexCount", sexCounts != null ? sexCounts : Collections.emptyList());
            put("gradeCount", gradeCounts != null ? gradeCounts : Collections.emptyList());
        }};

        return R.success(result);
    }
}
